from django.db import models
from flexy.sysmodel.models import *
from django.db import transaction
from flexy.common.db import *
   
class ObjectManager(models.Manager):
    @transaction.commit_manually
    def add(self, dict, flexyclass_id) :
        ret = err[6][0].copy()
        
        try :
            obj = self.model(flexyclass_id = flexyclass_id)
            obj.save()
            for field_id in dict :
                if ret['rc'] != 0 :
                    break
                if field_id == 'csrfmiddlewaretoken' :
                    continue
                field = Field.objects.get(id=field_id)
                val = None
                try :
                    val = refine_val(field.type, dict[field_id])
                except ValueError :
                    ret = err[6][2].copy()
                    ret['rm'] = ret['rm'] + str(field.name)
                try :
                    print field.name + " = " + str(val) 
                    f = Field_Table_Dict[field.type] \
                        (field_id = field_id, obj_id = obj.id, val = val)
                    f.save()
                except IntegrityError :
                    ret = err[6][1].copy()
                    ret['rm'] = str(field.name) + " " + val + ret['rm']
        finally :
            if ret['rc'] == 0 :
                transaction.commit()
            else :
                transaction.rollback()
            return ret

class Object(models.Model):
    flexyclass_id = models.IntegerField()
    objects = ObjectManager()

class Int(models.Model):
    field_id = models.IntegerField()
    obj_id = models.IntegerField()
    val = models.IntegerField(null=True)
    class Meta:
        unique_together = (('field_id', 'obj_id'),)
    
class Float(models.Model):
    field_id = models.IntegerField()
    obj_id = models.IntegerField()
    val = models.FloatField(null=True)
    class Meta:
        unique_together = (('field_id', 'obj_id'),)
      
class Char(models.Model):
    field_id = models.IntegerField()
    obj_id = models.IntegerField()
    val = models.CharField(max_length=255, null=True)
    class Meta:
        unique_together = (('field_id', 'obj_id'),)

class Text(models.Model):
    field_id = models.IntegerField()
    obj_id = models.IntegerField()
    val = models.TextField(null=True)
    class Meta:
        unique_together = (('field_id', 'obj_id'),)

class Char_Uniq(models.Model):
    field_id = models.IntegerField()
    obj_id = models.IntegerField()
    val = models.CharField(max_length=255, null=True)
    class Meta:
        unique_together = (('field_id', 'obj_id'), ('field_id', 'val'),)

class NTO1(models.Model):
    field_id = models.IntegerField()
    obj_id = models.IntegerField()
    val = models.IntegerField(null=True)
    class Meta:
        unique_together = (('field_id', 'obj_id'),)
        
class Date(models.Model):
    field_id = models.IntegerField()
    obj_id = models.IntegerField()
    val = models.DateField(null=True)
    class Meta:
        unique_together = (('field_id', 'obj_id'),)
        
class Time(models.Model):
    field_id = models.IntegerField()
    obj_id = models.IntegerField()
    val = models.TimeField(null=True)
    class Meta:
        unique_together = (('field_id', 'obj_id'),)
        
Field_Table_Dict = \
{ 
    'INT'       : Int, 
    'FLOAT'     : Float,
    'CHAR'      : Char,
    'CHAR_UNIQ' : Char_Uniq,
    'TEXT'      : Text,
    'NTO1'      : NTO1,
    'DATE'      : Date,
    'TIME'      : Time
}